package com.nzy.tree;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 构建树
 *
 * @author ningzhaoyun
 */
public class TreeBuilder {

    public static void main(String[] args) {
        String json = "[{\"appId\":\"cwl-common\",\"component\":\"channel-manager/channel-control/size-control/index\",\"havePermissionId\":1,\"hidden\":0,\"icon\":\"user\",\"id\":1,\"name\":\"用户管理\",\"parentId\":-1,\"path\":\"rootUser\",\"title\":\"用户管理\",\"type\":0},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":2,\"name\":\"测试页面一\",\"parentId\":1,\"path\":\"testPageOne\",\"title\":\"测试页面一\",\"type\":1},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":3,\"name\":\"测试按钮\",\"parentId\":2,\"path\":\"testButton\",\"title\":\"测试按钮\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":5,\"name\":\"测试按钮二\",\"parentId\":2,\"path\":\"testButtonTwo\",\"title\":\"测试按钮二\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":6,\"name\":\"测试页面二\",\"parentId\":1,\"path\":\"testPageTwo\",\"title\":\"测试页面二\",\"type\":1},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":8,\"name\":\"用户管理\",\"parentId\":1,\"path\":\"muLuUserMan\",\"title\":\"用户管理\",\"type\":3},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":9,\"name\":\"角色管理\",\"parentId\":8,\"path\":\"pageRoleMan\",\"title\":\"角色管理\",\"type\":1},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":10,\"name\":\"查询角色\",\"parentId\":9,\"path\":\"pageRoleManNewquerybutton\",\"title\":\"查询角色\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":11,\"name\":\"角色配置权限\",\"parentId\":9,\"path\":\"pageRoleManNewpzqxbutton\",\"title\":\"角色配置权限 \",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":13,\"name\":\"编辑角色\",\"parentId\":9,\"path\":\"updateRole\",\"title\":\"编辑角色\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":14,\"name\":\"删除角色\",\"parentId\":9,\"path\":\"deleteRole\",\"title\":\"删除角色\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":15,\"name\":\"用户管理\",\"parentId\":8,\"path\":\"pageUserManage\",\"title\":\"用户管理\",\"type\":1},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":16,\"name\":\"查询用户\",\"parentId\":15,\"path\":\"queryUserButton\",\"title\":\"查询用户\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":17,\"name\":\"重置\",\"parentId\":15,\"path\":\"reset\",\"title\":\"重置\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":18,\"name\":\"创建用户\",\"parentId\":15,\"path\":\"createUser\",\"title\":\"创建用户\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":19,\"name\":\"导入用户\",\"parentId\":15,\"path\":\"importUser\",\"title\":\"导入用户\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":20,\"name\":\"角色配置\",\"parentId\":15,\"path\":\"roleSetting\",\"title\":\"角色配置\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":21,\"name\":\"编辑\",\"parentId\":15,\"path\":\"editUser\",\"title\":\"编辑\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":22,\"name\":\"锁定\",\"parentId\":15,\"path\":\"lockUser\",\"title\":\"锁定\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":23,\"name\":\"重置密码\",\"parentId\":15,\"path\":\"resetPassword\",\"title\":\"重置密码\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":24,\"name\":\"删除\",\"parentId\":15,\"path\":\"deleteUser\",\"title\":\"删除\",\"type\":2},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":25,\"name\":\"参数设置\",\"parentId\":1,\"path\":\"muLuparamSetting\",\"title\":\"参数设置\",\"type\":3},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":26,\"name\":\"系统参数设置\",\"parentId\":25,\"path\":\"pageSystemParamSetting\",\"title\":\"系统参数设置\",\"type\":1},{\"appId\":\"cwl-common\",\"havePermissionId\":1,\"hidden\":0,\"id\":27,\"name\":\"保存\",\"parentId\":26,\"path\":\"save\",\"title\":\"保存\",\"type\":2}]";
        List<RoleViewVo> list = JSON.parseArray(json, RoleViewVo.class);
        List<RoleViewVo> result = parseTree(list);
        System.out.println(JSON.toJSONString(result));

    }

    public static List<RoleViewVo> parseTree(List<RoleViewVo> list) {
        Map<Long, List<RoleViewVo>> result = new HashMap<>(16);
        List<RoleViewVo> roots = new ArrayList<>();
        for (RoleViewVo vo : list) {
            Long parentId = vo.getParentId();
            if (result.get(parentId) == null) {
                List<RoleViewVo> vos = new ArrayList<>();
                vos.add(vo);
                result.put(parentId, vos);
            } else {
                result.get(parentId).add(vo);
            }
        }

        if (result.get(Long.valueOf(-1)) != null) {
            roots = result.get(-1L);
            for (RoleViewVo vo : roots) {
                parseChildren(vo, result);
            }
        }
        return roots;
    }

    public static void parseChildren(RoleViewVo parent, Map<Long, List<RoleViewVo>> map) {
        if (parent == null) {
            return;
        }
        List<RoleViewVo> vos = map.get(parent.getId());
        if (vos == null) {
            return;
        }

        List<RolePermissionButtonVo> buttons = null;
        for (RoleViewVo vo : vos) {
            if(vo.getType() != 2){
                break;
            }
            if (buttons == null) {
                buttons = new ArrayList<>();
            }
            RolePermissionButtonVo newObj = new RolePermissionButtonVo();
            newObj.setId(vo.getId());
            newObj.setKey(vo.getPath());
            newObj.setName(vo.getTitle());
            newObj.setHavePermissionId(vo.getHavePermissionId());
            newObj.setIndex(vo.getNum());
            buttons.add(newObj);
        }
        if (buttons != null) {
            parent.setButtons(buttons);
            parent.setChildren(null);
        } else {
            parent.setChildren(vos);
        }


        for (RoleViewVo vo : vos) {
            parseChildren(vo, map);
        }
    }

    private TreeBuilder(){}
}
